﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LDCRNewEdit.Model;
using System.IO;
using LDCRNewEdit.Utils;
using System.Drawing.Imaging;
using LDCRNewEdit.Services;

namespace LDCRNewEdit.UI.地图编辑.场景地图设计
{
    public partial class SceneGroundForm : Form
    {
        private const int WIDTH = 400;  //分割图片固定大小
        private const int HEIGHT = 300;

        public SceneGroundForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string filename = this.textBoxFilename.Text.Trim();
            string groundname = this.textBoxName.Text.Trim();

            this.button1.Enabled = false;

            try
            {

                if (string.IsNullOrEmpty(filename))
                {
                    MessageBox.Show("请选择地面JPG文件");
                }
                else if (string.IsNullOrEmpty(groundname))
                {
                    MessageBox.Show("请输入地面名称!");
                }
                else
                {
                    //分割图片
                    List<SceneGroundLv> list = SplitImage(filename);
                    if (list.Count == 0)
                    {
                        MessageBox.Show("无法生成，请选择另外的图片!");
                    }
                    else
                    {
                        //保存为文件
                        string fullpath = Resources.GetPath("SceneGround") + groundname;

                        if (File.Exists(fullpath))
                        {
                            if (MessageBox.Show("已经存在相同的地面，是否覆盖？", "提示", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.Cancel)
                            {
                                return;
                            }
                        }

                        SceneMapService.SaveGroundMap(fullpath, list);

                        MessageBox.Show("生成成功!");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现异常!" + ex.Message);
            }
            finally
            {
                this.button1.Enabled = true;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "*.jpg|*.jpg;*.jpeg";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBoxFilename.Text = ofd.FileName;
            }
        }

        /// <summary>
        /// 分割图片
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        private List<SceneGroundLv> SplitImage(string filename)
        {
            Image image = System.Drawing.Image.FromFile(filename);
            int w = image.Width;
            int h = image.Height;

            List<SceneGroundLv> list = new List<SceneGroundLv>();

            for (int y = 0; y < h; y += HEIGHT)
            {
                for (int x = 0; x < w; x += WIDTH)
                {
                    Bitmap bmp = new Bitmap(WIDTH, HEIGHT, PixelFormat.Format24bppRgb);
                    Graphics grap = Graphics.FromImage(bmp);
                    //grap.Clear(Color.Brown);
                    grap.DrawImage(image, new Rectangle(0, 0, WIDTH, HEIGHT), new Rectangle(x, y, WIDTH, HEIGHT), GraphicsUnit.Pixel);
                    grap.Save();

                    SceneGroundLv sg = new SceneGroundLv();
                    sg.X = x;
                    sg.Y = y;
                    sg.Image = bmp;

                    list.Add(sg);

                    //bmp.Save(string.Format("{0}_{1}.jpg", x, y), ImageFormat.Jpeg);
                }
            }
            image.Dispose();
            return list;
  
        }
    }
}
